package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.IntegrateRecordDao;
import com.fuli.app.dto.IntegrateRecordDTO;
import com.fuli.app.dto.ReviewDTO;
import com.fuli.app.dto.query.IntegrateRecordQueryDTO;
import com.fuli.app.entity.IntegrateRecord;
import com.fuli.app.entity.Review;
import com.fuli.app.service.IntegrateRecordService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntegrateRecordServiceImpl implements IntegrateRecordService {
    @Autowired
    private IntegrateRecordDao integrateRecordDao;
    @Override
    public ActionResult<String> add(IntegrateRecordDTO dto) {
        IntegrateRecord integrateRecord = EntityUtils.copyObjectWithInit(dto, IntegrateRecord.class);
        integrateRecord.setId(EntityUtils.uuid());
        integrateRecordDao.insert(integrateRecord);
        return new ActionResult<>(integrateRecord.getId());
    }

    @Override
    public ActionResult<String> delete(String id) {
        IntegrateRecord integrateRecord = integrateRecordDao.get(id);
        if (integrateRecord == null)
            return new ActionResult<>(id);
        integrateRecordDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDelete(List<String> ids) {
        integrateRecordDao.batchDelete(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<IntegrateRecordDTO> get(String id) {
        IntegrateRecord integrateRecord =integrateRecordDao.get(id);
        IntegrateRecordDTO integrateRecordDTO = EntityUtils.copyObject(integrateRecord,IntegrateRecordDTO.class);
        return new ActionResult<>(integrateRecordDTO);
    }

    @Override
    public ActionResult<Pagination<IntegrateRecordDTO>> listByQueryDTO(IntegrateRecordQueryDTO queryDTO) {
        Pagination pagination = new Pagination(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = integrateRecordDao.getCountByQuery(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<IntegrateRecord> integrateRecords = integrateRecordDao.listByQueryDTO(queryDTO);
            List<IntegrateRecordDTO> dtos = EntityUtils.copyList(integrateRecords,IntegrateRecordDTO.class);
            pagination.setRows(dtos);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }
}
